package com.self.finance.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.self.finance.model.CashWithdrawals;
import com.self.member.dto.UserDto;
import com.self.member.feign.UserFeignClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.self.finance.mapper.CoinRechargeMapper;
import com.self.finance.model.CoinRecharge;
import com.self.finance.service.CoinRechargeService;
import org.springframework.util.CollectionUtils;

@Service
public class CoinRechargeServiceImpl extends ServiceImpl<CoinRechargeMapper, CoinRecharge> implements CoinRechargeService{

    @Autowired
    private UserFeignClient userFeignClient;

    @Override
    public Page<CoinRecharge> findByPage(Page<CoinRecharge> page, Long coinId, Long userId, String userName, String mobile, Byte status, String numMin, String numMax, String startTime, String endTime) {
        LambdaQueryWrapper<CoinRecharge> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        Map<Long, UserDto> basicUsersByUsername = null;
        if (userId != null || StringUtils.isNotEmpty(userName) || StringUtils.isNotEmpty(mobile)){
            //查询用户名和真实姓名
            basicUsersByUsername = userFeignClient.getBasicUsersByUsername(userId == null ? null : Arrays.asList(userId), userName, mobile);
            if (!CollectionUtils.isEmpty(basicUsersByUsername)){
                lambdaQueryWrapper.in(!CollectionUtils.isEmpty(basicUsersByUsername.keySet()),CoinRecharge::getUserId,basicUsersByUsername.keySet());
            }
        }
        lambdaQueryWrapper.eq(status != null,CoinRecharge::getStatus,status)
                .between(!StringUtils.isEmpty(numMin) || !StringUtils.isEmpty(numMax),
                        CoinRecharge::getAmount,
                        StringUtils.isEmpty(numMin)? BigDecimal.ZERO:new BigDecimal(numMin),
                        StringUtils.isEmpty(numMax)? BigDecimal.ZERO:new BigDecimal(numMax))
                .between(!StringUtils.isEmpty(startTime) || !StringUtils.isEmpty(endTime),
                        CoinRecharge::getCreated,
                        StringUtils.isEmpty(startTime)? DateUtil.beginOfDay(new Date()).toJdkDate() :DateUtil.parse(startTime).toJdkDate(),
                        StringUtils.isEmpty(endTime)? DateUtil.endOfDay(new Date()).toJdkDate() :DateUtil.parse(endTime).toJdkDate());

        Page<CoinRecharge> rechargePage = page(page, lambdaQueryWrapper);
        if (rechargePage == null || CollectionUtils.isEmpty(rechargePage.getRecords())){
            return page;
        }
        if (CollectionUtils.isEmpty(basicUsersByUsername)){
            List<Long> collect = rechargePage.getRecords().stream().map(CoinRecharge::getUserId).collect(Collectors.toList());
            basicUsersByUsername = userFeignClient.getBasicUsersByUsername(collect, null, null);
        }
        Map<Long, UserDto> finalBasicUsersByUsername = basicUsersByUsername;
        if (!CollectionUtils.isEmpty(basicUsersByUsername)){
            rechargePage.getRecords().forEach(x -> {
                UserDto userDto = finalBasicUsersByUsername.get(x.getUserId());
                if (userDto != null){
                    x.setUsername(userDto.getUsername());
                    x.setRealName(userDto.getRealName());
                }
            });
        }
        return rechargePage;
    }
}
